package com.ict.txmsv2.services;

import com.ict.txmsv2.dto.StudentInfoQueryDto;
import com.ict.txmsv2.mapper.CompetitionPrizeMapper;
import com.ict.txmsv2.model.CompetitionPrize;
import com.ict.txmsv2.model.RespBean;
import com.ict.txmsv2.model.RespPageBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lht
 * @date 2021/4/22 - 13:52
 * @description: 竞赛奖励服务类
 */
@Service
public class CompetitionPrizeService {

    @Resource
    private CompetitionPrizeMapper competitionPrizeMapper;

    /**
     * 保存一个竞赛奖励信息
     * @param competitionPrize
     * @return
     */
    public RespBean save(CompetitionPrize competitionPrize) {
        if(competitionPrizeMapper.save(competitionPrize) != 0) {
            return RespBean.ok("保存成功！");
        }
        return RespBean.error("保存失败！");
    }

    public RespPageBean list(Integer page, Integer size, StudentInfoQueryDto queryDto) {
        if(page!=null && page>0){
            page = (page - 1)*size;
        }
        RespPageBean resp = new RespPageBean();
        List<CompetitionPrize> list = competitionPrizeMapper.list(page, size, queryDto);
        resp.setData(list);
        resp.setTotal(competitionPrizeMapper.getTotal(queryDto));
        return resp;
    }

    /**
     * 通过id删除一条记录
     * @param id
     * @return
     */
    public RespBean deleteCompetitionPrizeById(Integer id) {
        if (competitionPrizeMapper.deleteById(id) != 0) {
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败！");
    }

    public RespBean updateCompetitionPrize(CompetitionPrize competitionPrize) {
        if (competitionPrizeMapper.update(competitionPrize) != 0) {
            return RespBean.ok("更新成功!");
        }
        return RespBean.error("更新失败");
    }

    public RespPageBean getCompetitionPrizeEmpByName(Integer page, Integer size, String name) {
        if(page != null && page > 0){
            page = (page - 1)*size;
        }
        RespPageBean resp = new RespPageBean();
        List<CompetitionPrize> list = competitionPrizeMapper.getCompetitionPrizeEmpByName(page, size, name);
        resp.setData(list);
        resp.setTotal(competitionPrizeMapper.getTotalByName(name));
        return resp;
    }

    /**
     * 批量插入
     * @param list
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSave(List<CompetitionPrize> list) {
        return competitionPrizeMapper.batchSave(list) != 0;
    }

    public List<CompetitionPrize> getAllCompetitionPrizeInfo() {
        return competitionPrizeMapper.getAllCompetitionPrizeInfo();
    }
}
